/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp.home;

import it.francescogabbrielli.android.solidalapp.AbstractMapFragment;
import it.francescogabbrielli.android.solidalapp.DataEvent;
import it.francescogabbrielli.android.solidalapp.MapUtils;
import it.francescogabbrielli.android.solidalapp.R;
import it.francescogabbrielli.android.solidalapp.SolidalApp;
import it.francescogabbrielli.android.solidalapp.SolidalApp.DataKey;
import it.francescogabbrielli.android.solidalapp.requestendpoint.model.Request;
import android.util.Log;

import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.Marker;

/**
 * 
 */
public class RequestMapFragment extends AbstractMapFragment<HomeFragmentListener> {
	
	@Override
	public void onDataChanged(DataEvent event) {
		
		if (map==null || !isAdded())
			return;
		
		super.onDataChanged(event);
		
		Marker m = getMarker();
		if (m==null)
			return;
				
		DataKey key = event.getKey();
		
		switch(key) {
		
		// The address text has changed by reverse lookup
		case ADDRESS_TEXT:
			if (app.getData(DataKey.ADDRESS_TEXT_MANUAL)==null)
				updateMarker(m);
			break;
			
		// The request has been sent or is sending
		case REQUEST:
			if (event.isTriggered())
				updateMarker(m);
			break;
			
		default:
		}
		
//		if ("".equals(m.getTitle()))
//			System.out.println();
//		Log.i(SolidalApp.TAG, "TITLE="+m.getSnippet()+"; EVENT="+event+"; VALUE="+app.getData(key));
//		if(m.isInfoWindowShown())
//			m.showInfoWindow();
		
	}
	
	@Override
	public void onInfoWindowClick(Marker marker) {
		String snippet = marker.getSnippet();
		if(getString(R.string.map_marker_click).equals(snippet)) {
			marker.hideInfoWindow();
			marker.setSnippet("");
			interactionListener.send(marker.getPosition());
		} else if(getAcceptedSnippet().equals(snippet)) {
			interactionListener.openFile();
		}
	}
	
	private String getAcceptedSnippet() {
		return getString(R.string.map_marker_accepted, 
				"["+getString(R.string.open)+"]");
	}
	
	private void updateMarker(Marker m) {
		
		Request request = (Request) app.getData(DataKey.REQUEST);
		
		String snippet = "";
		String title = (String) app.getData(DataKey.ADDRESS_TEXT);
		if (interactionListener!=null && interactionListener.getRequest()!=null) {
			snippet = getString(R.string.map_marker_click);
			if (app.getData(DataKey.ADDRESS_TEXT_MANUAL)!=null)
				title = interactionListener.getRequest().getDetails();
		}
		if (request!=null && app.getData(DataKey.ADDRESS_TEXT_MANUAL)!=null)
			title = request.getDetails();
		
		try {
	//		boolean isAccepted = request!=null && request.getAccepted()!=null && request.getAccepted();
			boolean isExpired = request!=null && request.getExpired()!=null && request.getExpired();
			
			// request has been reset
			if (request==null || isExpired) {
	
				MapUtils.stopBlinkIcon(m, BitmapDescriptorFactory.HUE_RED);
				m.setDraggable(true);
				if (isExpired)
					snippet = getString(R.string.map_marker_expired);
				
			// request is just sending
			} else if (request.getId()==null) {
					
				m.setDraggable(false);
				MapUtils.startBlinkIcon(getActivity(), m, 900, Long.MAX_VALUE, new float[] {
					BitmapDescriptorFactory.HUE_GREEN, 
					BitmapDescriptorFactory.HUE_RED}
				);
				snippet= getString(R.string.map_marker_sending);
				
			// request has been sent (has the key...)
			} else if (!request.getAccepted()) {
				
				MapUtils.stopBlinkIcon(m, BitmapDescriptorFactory.HUE_GREEN);
				snippet = getString(R.string.map_marker_sent);
				
			// request sent and accepted
			} else {
				
				MapUtils.stopBlinkIcon(m, BitmapDescriptorFactory.HUE_AZURE);
				snippet = getAcceptedSnippet();
				
			}
	
			m.setTitle(title);
			m.setSnippet(snippet);
			if (mapShowInfo)
				m.showInfoWindow();
			
		} catch(Throwable t) {
			app.alert("ERROR: "+t);
			Log.e(SolidalApp.TAG, "Error", t);
		}
		
	}
	
}
